package cn.buk.tms.eotms.util;

import cn.buk.tms.eotms.entity.FlightInfo;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;

/**
 * @author yfdai
 */
public class BillUtil {

  private static Logger logger = Logger.getLogger(BillUtil.class);

  //LZHPEK/CA1876/S/2018-01-10 自定义的航班信息格式字符串 短格式
  final static private String REGEXP_SHORT = "[A-Z]{6}/[0-9A-Z]{3,8}/[A-Z-]/[0-9-]{10}";
  final static private Pattern PATTERN_SHORT = Pattern.compile(REGEXP_SHORT);

  //LZHPEK/CA1876/S/2018-01-10/2050/2350//T3 自定义的航班信息格式字符串 长格式
  final static private String REGEXP_LONG = "[A-Z]{6}/[0-9A-Z]{3,8}/[A-Z-]/[0-9-]{10}/[0-9]{0,4}/[0-9]{0,4}/[0-9A-Z]{0,3}/[0-9A-Z]{0,3}[/01]{0,2}";
  final static private Pattern PATTERN_LONG = Pattern.compile(REGEXP_LONG);

  public static void processRouteDetail(final String routeDetail0, final List<FlightInfo> flights) {
    // LZHPEK/CA1876/S/2018-01-10/2050/2350//T3
    if (routeDetail0 == null) {
      return;
    }

    String routeDetail = routeDetail0;
    Matcher m = PATTERN_LONG.matcher(routeDetail);

    int findCount = 0; // 找到的次数

    while (m.find()) {
      findCount++;
      final String temp = routeDetail.substring(m.start(), m.end());

      String[] items = temp.split("/");
      try {
        FlightInfo info = new FlightInfo();
        info.setDepartureAirport(items[0].substring(0, 3));
        info.setArrivalAirport(items[0].substring(3));

        info.setFlightNo(items[1]);
        info.setSubclass(items[2]);
        info.setDepartureDate(items[3]);

        if (items.length >= 6) {
          info.setDepartureTime(items[4]);
          info.setArrivalTime(items[5]);
        }

        if (items.length >= 7) {
          info.setDepartureTerminal(items[6]);
        }

        if (items.length >= 8) {
          info.setArrivalTerminal(items[7]);
        }

        if (items.length >= 9) {
          info.setFlightType(Integer.parseInt(items[8]));
        }

        flights.add(info);
      } catch (Exception ex) {
        logger.error(ex.getMessage() + ". :" + routeDetail);
      }

      routeDetail = routeDetail.substring(m.end());
      if (routeDetail.length() == 0) {
        break;
      } else {
        m = PATTERN_LONG.matcher(routeDetail);
      }
    }

    if (findCount == 0) {
      // 长格式没有，去匹配短格式
      processRouteDetailShort(routeDetail0, flights);
    }
  }

  public static void processRouteDetailShort(final String routeDetail0,
      final List<FlightInfo> flights) {
    // LZHPEK/CA1876/S/2018-01-10/

    String routeDetail = routeDetail0;
    Matcher m = PATTERN_SHORT.matcher(routeDetail);
    while (m.find()) {
      final String temp = routeDetail.substring(m.start(), m.end());
      String[] items = temp.split("/");
      try {
        FlightInfo info = new FlightInfo();
        info.setDepartureAirport(items[0].substring(0, 3));
        info.setArrivalAirport(items[0].substring(3));

        info.setFlightNo(items[1]);
        info.setSubclass(items[2]);
        info.setDepartureDate(items[3]);

        flights.add(info);
      } catch (Exception ex) {
        logger.error(ex.getMessage() + ". :" + routeDetail);
      }

      routeDetail = routeDetail.substring(m.end());
      if (routeDetail.length() == 0) {
        break;
      } else {
        m = PATTERN_SHORT.matcher(routeDetail);
      }
    }
  }

  /**
   * 保留2位精度
   */
  public static double decimalRoundHalfUp(Double f) {
    if (f == null) {
      return 0;
    }

    BigDecimal bd = BigDecimal.valueOf(f);
    bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);

    return bd.doubleValue();
  }

  public static int getYear(Date d) {
    Calendar c = new GregorianCalendar();
    c.clear();
    c.setTime(d);

    return c.get(Calendar.YEAR);
  }

  public static int getMonth(Date d) {
    Calendar c = new GregorianCalendar();
    c.clear();
    c.setTime(d);

    return c.get(Calendar.MONTH) + 1;
  }


}
